package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.AiKaForumDownLoader;
import com.spider.silence.output.FileWriter;
import com.spider.silence.output.FileWriterPool;
import com.spider.silence.output.MongoWriter;
import com.spider.silence.output.MongoWriterPool;
import com.spider.silence.pool.AiKaDownLoaderPool;
import com.spider.silence.task.AiKaForumTask;
import com.spider.silence.task.ZuiChengDuTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * @author 夏茂轩
 * @package com.spider.silence.worker
 * @Date 2017/3/8 20:14
 */
public class AiKaWorker  implements Runnable {
    private Logger logger = Logger.getLogger(AiKaWorker.class);

    @Override
    public void run() {
        while (true && !Thread.interrupted()){
            AiKaDownLoaderPool pool = AiKaDownLoaderPool.getInstance();
            AiKaForumDownLoader downLoader = null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downLoader=pool.fetchDownLoader(1000);
                AiKaForumTask task = (AiKaForumTask) CommonTaskManager.takeTask("AiKaForumTask",1000);
                if(task==null){
                    continue;
                }
                JSONObject content = downLoader.crawlItem(task.getUrl());
                String filePrefix = "AiKa";//文件前缀
                JSONObject data = content.getJSONObject("data");
                if(data.size() == 0)return;
                long tsp = DateUtils.dateToTimestamp(DateUtils.parseDateTimeOnModel(data.getString("publish_time"), DateUtils.DATETIME_FORMAT_MM)).getTime();
                String user_id = data.getString("publish_user_id");
                String tid = data.getString("id");
                String fileName = filePrefix + "_" + tsp + "_" + user_id + "_" + tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                String publishDate = DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-", "-");
                writer = writerPool.fetchWriter();
                Map<String,File> map=downLoader.getFiles();
                writer.write("爱卡论坛_speeches_" + publishDate, fileName, JSON.toJSONString(content, SerializerFeature.BrowserCompatible),map);
            } catch (InterruptedException e) {
                e.printStackTrace();
                logger.error(e.getMessage(),e);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage(),e);
            } finally {
                if(downLoader!=null){
                    pool.releaseDownLoader(downLoader);
                }
                if(writer!=null){
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}
